/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.Alquiler;
import negocio.Articulo;
import negocio.ArticuloAlquilado;
import negocio.FacturaAlquiler;
import negocio.FacturaCompra;

/**
 *
 * @author JAVA
 */
public class PersistenciaFactura {

    public static int insertarFacturaCompra(FacturaCompra f) {

        ArrayList<Articulo> listaArticulos = null;
        int i = 0;
        int idfactura=0;
        Connection con = null;
        Statement st = null;
        Date fecha = new Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fAlta = sdf.format(f.getFecha());

        try {


            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub", "cibernos", "1234");
            st = con.createStatement();
            i = st.executeUpdate("insert into factura (id, nombreVideoclub, iddependiente, nifCliente, nombreCliente, fecha, total, tipo) values"
                    + "(0,'" + f.getVideoclub() + "',"+f.getDependiente().getId()+ ",'" + f.getDni() + "','" + f.getNombre() + "','" + fAlta + "'," + f.getTotal() + ",'compra' )");

            if (i == 1) {

                listaArticulos = f.getCompra().getListaArticulos();
                Iterator it = listaArticulos.iterator();
                ResultSet r = st.executeQuery("select last_insert_id()");
                
                if (r.next()) {
                    idfactura=r.getInt(1);
                }


                while (it.hasNext()) {
                    Articulo a = (Articulo) it.next();
                    i = st.executeUpdate("insert into facturaarticulo (id,idArticulo, idFactura) values"
                            + "(0," +a.getId()+ "," +idfactura+ ")");
                }
                
            }
            st.close();
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return i;
        }
    }

    public static int insertarFacturaAlquiler(FacturaAlquiler f) {

        int i = 0;
        Connection con = null;
        int idfactura=0;
        List <ArticuloAlquilado> lista=null;
        Statement st = null;
        Date fecha = new Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fAlta = sdf.format(f.getFecha());

        try {


            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub", "cibernos", "1234");
            st = con.createStatement();
            i = st.executeUpdate("insert into factura (id, nombreVideoclub,iddependiente, nifCliente, nombreCliente, fecha, total, tipo) values"
                    + "(0,'" + f.getVideoclub() + "',   "+f.getDependiente().getId()+",'" + f.getCliente().getDni() + "','" + f.getCliente().getNombre() + "','" + fAlta + "'," + f.getTotal() + ",'alquiler')");
            
            if (i == 1) {

                lista=f.getAlquiler().getListArt();
                Iterator it = lista.iterator();
                
                ResultSet r = st.executeQuery("select last_insert_id()");
                
                if (r.next()) {
                    idfactura=r.getInt(1);
                }


                while (it.hasNext()) {
                    ArticuloAlquilado aa=(ArticuloAlquilado)it.next();                    
                    i = st.executeUpdate("insert into facturaarticulo (id,idArticulo, idFactura) values"
                            + "(0," +aa.getId()+ "," +idfactura+ ")");
                }
                
            }
            st.close();
            con.close();


        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaFactura.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return i;
        }
    }
}
